package com.nwpu.coursesupport.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.criteria.BatchDealAnswerSearchCriteria;
import com.nwpu.coursesupport.criteria.TaskSubmitResultSearchCriteria;
import com.nwpu.coursesupport.entity.Resource;
import com.nwpu.coursesupport.entity.bean.Files.FileItem;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.StatusType;
import com.nwpu.coursesupport.exception.BatchTaskPersonalException;
import com.nwpu.coursesupport.service.TaskSubmitResultService;
import com.nwpu.coursesupport.service.imple.ResourceService;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.service.param.TaskSubmitResultServiceParam;
import com.nwpu.coursesupport.tool.JSONUtils;
import com.nwpu.coursesupport.tool.WebUtils;

@Controller
@RequestMapping("/taskSubmitResult")
public class TaskSubmitResultController extends AbstractController {

	@Autowired
	private TaskSubmitResultService taskSubmitResult;
	
	@Autowired
	private ResourceService resourceService;

	@RequestMapping("/ajaxDetail")
	@ResponseBody
	public JSONResult ajaxDetail(@RequestParam("id")int id){
		JSONResult rst = new JSONResult();
		rst.fail();
		IDParam p=new IDParam();
		p.setId(id);
		try{
			rst = this.taskSubmitResult.detail(p);
		}catch(Exception e){
			logger.error("#####TaskSubmitResultControllerajax获取taskSubmitResult细节失败"+e.getMessage());
			return rst.fail("获取失败，请重试！");
		}
		return rst;
	}
	
	@RequestMapping("/detail")
	public String detail(@RequestParam("id")int id,HttpServletRequest request){
		JSONResult rst = new JSONResult();
		rst.fail();
		IDParam p=new IDParam();
		p.setId(id);
		try{
			rst = this.taskSubmitResult.detail(p);
			request.setAttribute("data", this.parseData(rst));
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController获取taskSubmitResult细节失败"+e.getMessage());
			request.setAttribute("data", this.parseData(rst));
		}
		return "/teapage/courseTaskCorrect";
	}
	
	
	@RequestMapping(value="/batchCreate",method=RequestMethod.POST)
    @ResponseBody
	public JSONResult batchCreate(@RequestParam("data")String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultServiceParam.BatchCreatePersonalTaskParam p = JSONUtils.json2pojo(data, TaskSubmitResultServiceParam.BatchCreatePersonalTaskParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.personalTaskBatchCreate(p);
		}catch(BatchTaskPersonalException e){
			logger.error("#####TaskSubmitResultController批量create失败"+e.getMessage());
			return rst.fail("创建失败，请重试！");
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController批量create失败"+e.getMessage());
			return rst.fail("创建失败，请重试！");
		}
		return rst;
	}
	
	@RequestMapping(value="/create",method=RequestMethod.POST)
    @ResponseBody
	public JSONResult create(@RequestParam("data")String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultServiceParam.CreatePersonalTaskParam p = JSONUtils.json2pojo(data, TaskSubmitResultServiceParam.CreatePersonalTaskParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.personalTaskCreate(p);
		}catch(BatchTaskPersonalException e){
			logger.error("#####TaskSubmitResultController执行create失败"+e.getMessage());
			return rst.fail("创建失败，请重试！");
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行create失败"+e.getMessage());
			return rst.fail("创建失败，请重试！");
		}
		return rst;
	}
	@RequestMapping("/ptListData")
	public String taskPersonalList(@RequestParam("taskId")int taskId,HttpServletRequest request){
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultSearchCriteria p = new TaskSubmitResultSearchCriteria();
		p.setAssignId(taskId);
		
		try{
			rst = this.taskSubmitResult.taskPersonalList(p);
			request.setAttribute("data", this.parseData(rst));
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行ptListData失败"+e.getMessage());
			request.setAttribute("data", this.parseData(rst));
		}
		return "/teapage/courseTaskPersonalPublish";
	}
	
	@RequestMapping("/ajaxPtListData")
    @ResponseBody
	public JSONResult ajaxPtListData(@RequestParam("data")String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultSearchCriteria p = JSONUtils.json2pojo(data, TaskSubmitResultSearchCriteria.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.taskPersonalList(p);
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行ptListData失败"+e.getMessage());
			return rst.fail("获取失败，请重试！");
		}
		return rst;
	}
	
	@RequestMapping("/loadReview")
	public String listReview(@RequestParam("assignId")int assignId,HttpServletRequest request){
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultSearchCriteria p = new TaskSubmitResultSearchCriteria();
		p.setAssignId(assignId);
		try{
			rst = this.taskSubmitResult.taskMainList(p);
			request.setAttribute("data", this.parseData(rst));
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行loadReview失败"+e.getMessage());
			rst.setMessage("获取失败，请重试！");
			request.setAttribute("data", this.parseData(rst));
		}
		return "/teapage/courseTaskReview";
	}
	
	@RequestMapping("/mtlistData")
	public String list(@RequestParam("assignId")int assignId,HttpServletRequest request){
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultSearchCriteria p = new TaskSubmitResultSearchCriteria();
		p.setAssignId(assignId);
		try{
			rst = this.taskSubmitResult.taskMainList(p);
			request.setAttribute("data", this.parseData(rst));
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行mtlistData失败"+e.getMessage());
			rst.setMessage("获取失败，请重试！");
			request.setAttribute("data", this.parseData(rst));
		}
		return "/teapage/courseTaskSubmit";
	}
	
	@RequestMapping("/ajaxMtlistData")
    @ResponseBody
	public JSONResult ajaxMtlistData(@RequestParam("data")String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultSearchCriteria p = JSONUtils.json2pojo(data, TaskSubmitResultSearchCriteria.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.taskMainList(p);
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行ajaxmtlistData失败"+e.getMessage());
			return rst.fail("获取失败，请重试！");
		}
		return rst;
	}
	
	@RequestMapping("/searchUser")
    @ResponseBody
	public JSONResult searchUser(@RequestParam("data")String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultServiceParam.SearchUserParam p = JSONUtils.json2pojo(data, TaskSubmitResultServiceParam.SearchUserParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.searchUser(p);
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行searchUser失败"+e.getMessage());
			return rst.fail("搜索失败，请重试！");
		}
		return rst;
	}
	
	@RequestMapping(value="/dealAnswerVisible",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult dealAnswerVisible(@RequestParam("data") String data) {
    	JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultServiceParam.DealAnswerVisible p = JSONUtils.json2pojo(data, TaskSubmitResultServiceParam.DealAnswerVisible.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.updateAnswerStatus(p);
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行dealAnswerVisible失败");
			return rst.fail("处理失败，请重试！");
		}
		return rst;
    }
	
	@RequestMapping(value="/shareSubmit",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult shareSubmit(@RequestParam("data") String data) {
    	JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultServiceParam.ShareSubmitParam p = JSONUtils.json2pojo(data, TaskSubmitResultServiceParam.ShareSubmitParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.shareSubmit(p);
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行shareSubmit失败");
			return rst.fail("处理失败，请重试！");
		}
		return rst;
    }
    
    @RequestMapping(value="/batchDealAnswerVisible",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult batchDealAnswerVisible(@RequestParam("data") String data) {
    	JSONResult rst = new JSONResult();
		rst.fail();
		BatchDealAnswerSearchCriteria p = JSONUtils.json2pojo(data,BatchDealAnswerSearchCriteria.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.batchUpdateAnswerStatus(p);
		}catch(Exception e){
			logger.error("修改失败");
			return rst.fail("后台出错");
		}
		return rst;
    }
    
    @RequestMapping("/review")
    @ResponseBody
    public JSONResult review(@RequestParam("data") String data) {
    	JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultServiceParam.ReviewParam p = JSONUtils.json2pojo(data, TaskSubmitResultServiceParam.ReviewParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.review(p);
		}catch(Exception e){
			logger.error("#####为id="+p.getId()+"的学生评分失败");
			return rst.fail("评分出错！请重新尝试");
		}
		return rst;
    }
    
    @RequestMapping("/batchReview")
    @ResponseBody
    public JSONResult batchReview(@RequestParam("data") String data) {
    	JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultServiceParam.BatchReviewParam p = JSONUtils.json2pojo(data, TaskSubmitResultServiceParam.BatchReviewParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.batchReview(p);
		}catch(Exception e){
			logger.error("#####批量评分失败(from taskSubmitResult.batchReview())");
			return rst.fail("评分出错！请重新尝试");
		}
		return rst;
    }
    
    @RequestMapping("/submit")
    @ResponseBody
    public JSONResult submit(@RequestParam("data") String data) throws Exception {
    	JSONResult rst = new JSONResult();
		rst.fail();
		TaskSubmitResultServiceParam.SubmitParam p = JSONUtils.json2pojo(data, TaskSubmitResultServiceParam.SubmitParam.class);
		if (p == null) {
			return rst.fail("参数转换有误");
		}
//		try{
//			rst = this.taskSubmitResult.submit(p);
//		}catch(Exception e){
//			logger.error("#####TaskSubmitResultController执行submit失败");
//			return rst.fail("处理失败，请重试！");
//		}

		rst = this.taskSubmitResult.submit(p);
		return rst;
    }
    
    @RequestMapping("/editUploadFile")
    @ResponseBody
    public JSONResult reUploadFile(@RequestParam("data")String data){
    	JSONResult rst = new JSONResult();
    	rst.fail();
    	TaskSubmitResultServiceParam.SubmitParam p = JSONUtils.json2pojo(data, TaskSubmitResultServiceParam.SubmitParam.class);
    	if (p == null) {
			return rst.fail("参数转换有误");
		}
		try{
			rst = this.taskSubmitResult.reUploadFile(p);
		}catch(Exception e){
			logger.error("#####TaskSubmitResultController执行editUploadFile失败");
			return rst.fail("补交附件失败，请重试！");
		}
    	return rst;
    }    
    
    @RequestMapping(value="/changeTaskSubmitStatus",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult changeTaskSubmitStatus(@RequestParam("assignId") int assignId){
    	JSONResult rst = new JSONResult();
    	rst.fail();
    	IDParam p = new IDParam();
    	p.setId(assignId);    	
		try{
			rst = this.taskSubmitResult.changeStatus(p);			
		}catch(Exception e){
			logger.error("更改任务提交状态失败"+e.getMessage());
			return rst.fail("更改任务提交状态失败");
		}
    	return rst;    	
    }
    
    @RequestMapping("/downloadAll")
    @ResponseBody
    public void downloadAll(@RequestParam("assignId") int assignId, HttpServletResponse response) throws IOException{
		
		IDParam p = new IDParam();
    	p.setId(assignId);
    	
    	String name = this.taskSubmitResult.getInfoZip(assignId);
		List<FileItem> filist = this.taskSubmitResult.getAllFileUrl(p);
		//获取文件名和路径
		if(filist.size() < 1){
			WebUtils.sendJson(response,StatusType.WRONG,"没有可下载文件");
		}else{
			List<Resource> relist = this.resourceService.getNameUrl(filist);
			if(relist.size() < 1){
				logger.error("压缩文件出错");
				WebUtils.sendJson(response,StatusType.WRONG,"压缩文件出错");
			}else{
				String path = this.getSetting().getUploadPath();
				WebUtils.sendFileList(response, relist, path, name);
			}			
		}  	
    }

	@RequestMapping(value="/ajaxDelete",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult ajaxDelete(@RequestParam("id") Integer id) {
		JSONResult rst = new JSONResult();
		rst.fail();

		IDParam p = new IDParam();
		p.setId(id);
		try {
			rst = this.taskSubmitResult.remove(p);
		} catch (Exception e) {
			logger.error("分派记录删除失败");
			return rst.fail("后台出错");
		}
		return rst;
	}

}
